﻿
@{
    ViewBag.Title = "GuidanceAdd";
    Layout = "~/Views/Shared/layoutCommon.cshtml";
}

@model OF.Component.DTO.GuidanceDto

@section style{
    <style>
        .form-horizontal .control-label {
            width: 20%;
            float: left;
            text-align: right;
        }

        .col-sm-3 {
            width: 80%;
            float: left;
        }

            .col-sm-3 select {
                float: left;
                width: 150px;
                margin-left: 5px;
            }
    </style>
}
<form id="form1" class="form-horizontal">
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box-body">
                    <div data-toggle="distpicker" class="form-group" data-value-type="code">
                        <label class="col-sm-2 control-label" for="name">城市：</label>
                        <div class="col-sm-3">
                            <select name="ProvinceID" id="ProvinceID" class="form-control" data-province="@Model.ProvinceId"></select>
                            <select name="CityID" id="CityID" class="form-control" data-city="@Model.CityID"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">分类：</label>
                        <div class="col-sm-3">
                            <select class="form-control" style="width:150px;" name="selMasterBizType" id="selMasterBizType"></select>
                            <select class="form-control" style="width:150px;" name="selBizType" id="selBizType"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">内容：</label>
                        <div class="col-sm-3">
                            <textarea id="txtGuidance" name="txtGuidance" class="ueditor-80p"></textarea>
                            <input type="hidden" id="hidGuidance" value="@Model.GuidanceHtml" name="hidGuidance" />
                        </div>
                    </div>
                </div>
                <div class="box-footer text-center">
                    <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                    <button class="btn" type="button" id="cancel">取 消 </button>
                </div>
            </div>

        </div>
        <!-- /.row -->
    </section>

</form>
@section scriptsBottom{
    <script src="@Url.Content("~/ueditor/ueditor.config.js")"></script>
    <script src="@Url.Content("~/ueditor/ueditor.all.min.js")"></script>
    <script src="@Url.Content("~/ueditor/lang/zh-cn/zh-cn.js")"></script>
    <script type="text/javascript">
        $(document).ready(function () {
           renderUeditor();
           initFormValidate();
           var mid = @Model.MasterBizTypeId;
           var bid = @Model.BizTypeID;
           initMasterBizType(mid);
           initBindBizType();
           if(bid>0)
           {
               bindBizType(mid,bid);
           }
           $("#btnSave").click(function(){
               save();
           });
           $("#cancel").click(function () {
               BMCW.closeTopLayer();
           });
       });
        function initFormValidate() {
           $("#form1").bootstrapValidator({
               submitHandler: function (valiadtor, loginForm, submitButton) {
                   valiadtor.defaultSubmit();
               },
               fields: {
                   CityID: {
                       validators: {
                           notEmpty: {
                               message: '请选择城市'
                           }
                       }
                   },
                   selBizType: {
                       validators: {
                           notEmpty: {
                               message: '请选择分类'
                           }
                       }
                   }

               }
           });
       }

       //保存
       function save()
        {
           var bootstrapValidator = $("#form1").data('bootstrapValidator');
           bootstrapValidator.validate();
           if (bootstrapValidator.isValid()) {
               if(UE.getEditor('txtGuidance').getContent()=="")
               {
                   BMCW.alert("内容不能为空！");
                   return;
               }
               var url = '@Url.Action("SaveGuidance", "BasicData")'
               BMCW.ajax({
                   url: url,
                   type: "Post",
                   contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                   data: {
                       GuidanceID:@Model.GuidanceID,
                       CityID: $("#CityID").val(),
                       BizTypeID: $("#selBizType").val(),
                       GuidanceHtml: ajax_encode(UE.getEditor('txtGuidance').getContent()),
                       GuidanceWxMini: UE.getEditor('txtGuidance').getPlainTxt()
                   },
                   success: function (o) {
                       if (o.status == 0) {
                           BMCW.msg("保存成功",function(){
                               BMCW.getTopSelectedWin().search();
                           });
                       } else {
                           BMCW.alert("保存失败");
                       }
                   }
               });
           }
       }

        //一级分类
        function initMasterBizType(mid)
        {
            $("#selMasterBizType").append("<option value='0'>请选择主分类</option>");
             var url = '@Url.Action("GetMasterBizTypeList", "BasicData")'
            BMCW.ajax({
                url: url,
                type: "GET",
                success: function (o) {
                    if (o.status == 0) {
                        $.each(o.data, function (i) {
                            var sel = "";
                            if (o.data[i].MasterBizTypeID == mid) {
                                sel = "selected='true'";
                            }
                            $("#selMasterBizType").append("<option value='" + o.data[i].MasterBizTypeID + "' " + sel + ">" + o.data[i].MasterBizTypeName + "</option>");
                        });
                    }
                }
            });
        }

        function initBindBizType()
        {
            $("#selBizType").append("<option value=''>请选择子类</option>");
            $("#selMasterBizType").bind("change", function () {
                var typeId = $(this).val();
                bindBizType(typeId);
            });
        }

        function bindBizType(mid,typeId)
        {
            var url = '@Url.Action("GetBizTypeListByMasterId", "BasicData")'
            BMCW.ajax({
                url: url,
                data: { "masterId": mid },
                type: "GET",
                success: function (rd) {
                    if (rd.status == 0) {
                        $("#selBizType").empty();
                        $("#selBizType").append("<option value=''>请选择子类</option>");
                        var dataObj = rd.data;
                        for (var i = 0; i < dataObj.length; i++) {
                            var bizTypeId = dataObj[i].BizTypeID;
                            var bizTypeName = dataObj[i].BizTypeName;
                            var sel = "";
                            if (bizTypeId == typeId) {
                                sel = "selected='true'";
                            }
                            $("#selBizType").append("<option value='" + bizTypeId + "' " + sel + ">" + bizTypeName + "</option>");
                        }
                    }
                }
            });
        }

        function renderUeditor() {
            UE.getEditor("txtGuidance");
            var hidGuidance = $("#hidGuidance");
            if (hidGuidance.val() != '') {
                UE.getEditor("txtGuidance").ready(function () {
                    this.setContent(hidGuidance.val());
                });
            }
        }

        function ajax_encode(str) {
            str = str.replace(/%/g, "{$bai$}");
            str = str.replace(/ /g, "{$kong$}");
            str = str.replace(/</g, "{$zuojian$}");
            str = str.replace(/>/g, "{$youjian$}");
            str = str.replace(/&/g, "{$and$}");
            str = str.replace(/\"/g, "{$shuang$}");
            str = str.replace(/\'/g, "{$dan$}");
            str = str.replace(/\t/g, "{$tab$}");
            str = str.replace(/\+/g, "{$jia$}");
            return str;
        }
    </script>
}
